//
//  AuthenResultViewController.m
//  YTwallet
//
//  Created by ZhangLin on 2019/3/11.
//  Copyright © 2019年 hadrlog. All rights reserved.
//

#import "AuthenResultViewController.h"
#import "AuthenResultTableViewCell.h"
#import "AuthenResultInfoViewController.h"
//认证失败
#import "AuthenFailViewController.h"
//高级认证
#import "AdvancedIdentifyViewController.h"
//获取实名认证信息
#import "GetUserInfoModelPack.h"

@interface AuthenResultViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *imageArray;
@property(nonatomic,strong)NSMutableArray *titleArray;
@property(nonatomic,strong)NSMutableArray *tipsArray;

@end

@implementation AuthenResultViewController

-(NSMutableArray *)tipsArray
{
    if (_tipsArray == nil) {
        _tipsArray = [NSMutableArray array];
    }
    return _tipsArray;
}

-(NSMutableArray *)titleArray
{
    if (_titleArray == nil) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}

-(NSMutableArray *)imageArray
{
    if (_imageArray == nil) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"实名认证";
    self.view.backgroundColor = [UIColor colorWithRed:242 / 255.0 green:244 / 255.0 blue:246 / 255.0 alpha:1];
    //创建tableView
    [self createTableView];
    
    //删除认证页面填写页面  从"我的"跳转过来的不能删除
    if (self.navigationController.viewControllers.count > 2) {
        [self deleteVC];
    }
    
}
-(void)deleteVC
{
    NSMutableArray *array = [self.navigationController.viewControllers mutableCopy];
    //把不需要的页面删除
    NSArray *arrayB = @[@"1",@"2"];
    for (int j = 0; j < arrayB.count; j++) {
        NSString *a = [arrayB objectAtIndex:j];
        int b = [a intValue] - j;
        [array removeObjectAtIndex:b];
    }
    
    //把删除后的控制器数组再次复制
    [self.navigationController setViewControllers:[array copy]];
}
-(void)createTableView
{
    self.imageArray = @[@"icon_renzheng_green",@"icon_renzheng_purple"].mutableCopy;
//    self.titleArray = @[@"基础认证",@"高级认证"].mutableCopy;
    self.titleArray = @[@"基础认证"].mutableCopy;
    self.tipsArray = @[@"认证后\n开启信用卡收款功能",@"认证后\n开启所有功能"].mutableCopy;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, 200) style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[AuthenResultTableViewCell class] forCellReuseIdentifier:@"cell"];
//    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.scrollEnabled = NO;
    [self.view addSubview:self.tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titleArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AuthenResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.imv.image = [UIImage imageNamed:self.imageArray[indexPath.row]];
    cell.titleLab.text = self.titleArray[indexPath.row];
    cell.tipsLab.text = self.tipsArray[indexPath.row];
    //点击不变色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        cell.toAuthenBtn.hidden = YES;
        cell.authenStatusLab.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [cell.authenStatusLab addGestureRecognizer:tap];
        
        cell.arrowImv.userInteractionEnabled = YES;
        UITapGestureRecognizer *arrowTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [cell.arrowImv addGestureRecognizer:arrowTap];
    }else
    {
        cell.authenStatusLab.hidden = YES;
        cell.arrowImv.hidden = YES;
        [cell.toAuthenBtn addTarget:self action:@selector(toAuthenBtnAction)
                   forControlEvents:(UIControlEventTouchUpInside)];
    }
    
    
    return cell;
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

//基础认证状态查看
-(void)tapAction
{
    NSLog(@"点击基础认证");
    
    if (self.accountName.length != 0) {
        AuthenResultInfoViewController *authenVC = [[AuthenResultInfoViewController alloc] init];
        authenVC.mobile = self.mobile;
        authenVC.crpIdenNumber = self.crpIdenNumber;
        
        authenVC.accountName = self.accountName;
        authenVC.bankReservedMobile = self.bankReservedMobile;
        authenVC.accountNo = self.accountNo;
        authenVC.openBankName = self.openBankName;
        [self.navigationController pushViewController:authenVC animated:YES];
    }else
    {
        //实名认证完成需要重新查询
        [self basicAuthenedBtnAction];
    }
    
    
    
    
    
    /*  认证失败跳转
    AuthenFailViewController *authenFailVC = [[AuthenFailViewController alloc] init];
    [self.navigationController pushViewController:authenFailVC animated:YES];
     */
}

-(void)basicAuthenedBtnAction
{
    NSLog(@"点击已认证(基础认证)");
    
    NSString *url = [[ServerAddressModel shareInstance] fullTAnd0AddrWithParameter:@"/epay_apps/user/userInfoE0100"];
    [[ETONEHttpClient sharedInstance] doHTTPSPOST:url Parameters:nil HttpBody:nil SuccessHandler:^(id responseObject) {
        
        NSDictionary *dic = [responseObject mj_JSONObject];
        NSString *code = dic[@"code"];
        if ([@"0000" isEqualToString:code]) {
            [self dealUserInfoWithresponse:responseObject];
        }
        
    } FailedHandler:^(NSError *error) {
        
    }];
}
-(void)dealUserInfoWithresponse:(id)responseObject
{
    
    GetUserInfoResponse *getUserInfoResponse = [GetUserInfoResponse mj_objectWithKeyValues:responseObject];
    GetUserInfoResponseData *userInfo = getUserInfoResponse.response;
    
    
    AuthenResultInfoViewController *authenResultVC = [[AuthenResultInfoViewController alloc] init];
    authenResultVC.mobile = userInfo.mobile;
    authenResultVC.crpIdenNumber = userInfo.crpIdenNumber;
    
    authenResultVC.accountName = userInfo.accountName;
    authenResultVC.bankReservedMobile = userInfo.bankReservedMobile;
    authenResultVC.accountNo = userInfo.accountNo;
    authenResultVC.openBankName = userInfo.openBankName;
    [self.navigationController pushViewController:authenResultVC animated:YES];
}





-(void)toAuthenBtnAction
{
    NSLog(@"点击高级认证");
    [self showFailedResultTips:ETNODisparkStr animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
    /*
    AdvancedIdentifyViewController *advancedVC = [[AdvancedIdentifyViewController alloc] init];
    [self.navigationController pushViewController:advancedVC animated:YES];
     */
}

@end
